package com.mokee.android.ShipPass;

import com.mokee.Util.ShipAttrUtil;
import com.mokee.android.database.DataBaseHelper;
import com.mokee.android.database.UserTable;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ShipProcessItemDetailActivity extends Activity implements OnClickListener {
	private static final String tag = "ShipProcessItemDetailActivity";

	private Button btn_TopReturn;
	private TextView layout_sub_Title, iv_Submit;
	private LinearLayout layout_sub_Add;
	private LinearLayout linearLayout_MoneyDetail;
	private TextView tv_TicketNumber, tv_AllMoney, tv_MoneyType, tv_ShipWeight;
	private TextView tv_WeightNow, tv_LoadType, tv_SafeLevel, tv_SailStart, tv_SailEnd, tv_Sail_Orientation, tv_GoodsName;
	private TextView tv_ShipName, tv_ShipType, tv_ShipWeightOri, tv_RegisterNum;
	
	private UserTable table;
	private int ID;
	private int process;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_process_item_detail);
		
		Bundle data = getIntent().getBundleExtra("bundle");
		ID = data.getInt("id");
		
		table = DataBaseHelper.getInstance().getUserRecord().queryForId(ID);
		process = table.getShipProcess();
		
		initView();
		initEvent();
		loadingData();
	}
	
	private void initView() {
		btn_TopReturn = (Button) findViewById(R.id.btn_TopReturn);
		layout_sub_Title = (TextView) findViewById(R.id.layout_sub_Title);
		iv_Submit = (TextView) findViewById(R.id.iv_Submit);
		layout_sub_Add = (LinearLayout) findViewById(R.id.layout_sub_Add);
		
		linearLayout_MoneyDetail = (LinearLayout) findViewById(R.id.linearLayout_MoneyDetail);
		
		tv_TicketNumber = (TextView) findViewById(R.id.tv_TicketNumber);
		tv_AllMoney = (TextView) findViewById(R.id.tv_AllMoney);
		tv_MoneyType = (TextView) findViewById(R.id.tv_MoneyType);
		tv_ShipWeight = (TextView) findViewById(R.id.tv_ShipWeight);
		
		tv_WeightNow = (TextView) findViewById(R.id.tv_WeightNow);
		tv_LoadType = (TextView) findViewById(R.id.tv_LoadType);
		tv_SafeLevel = (TextView) findViewById(R.id.tv_SafeLevel);
		tv_SailStart = (TextView) findViewById(R.id.tv_SailStart);
		tv_SailEnd = (TextView) findViewById(R.id.tv_SailEnd);
		tv_Sail_Orientation = (TextView) findViewById(R.id.tv_Sail_Orientation);
		tv_GoodsName = (TextView) findViewById(R.id.tv_GoodsName);
		
		tv_ShipName = (TextView) findViewById(R.id.tv_ShipName);
		tv_ShipType = (TextView) findViewById(R.id.tv_ShipType);
		tv_ShipWeightOri = (TextView) findViewById(R.id.tv_ShipWeightOri);
		tv_RegisterNum = (TextView) findViewById(R.id.tv_RegisterNum);
	}
	
	private void initEvent() {
		btn_TopReturn.setOnClickListener(this);
		layout_sub_Add.setOnClickListener(this);
		layout_sub_Title.setText(R.string.textview_this_pay_detail_title_tip);
		setTopButtonText();
		setMoneyPanelVisibility();
	}
	
	private void loadingData() {
		if(process != 0){
			tv_TicketNumber.setText(table.getShipTicketNumber());
			tv_AllMoney.setText(String.valueOf(table.getShipPayMoney()));
			tv_MoneyType.setText(String.valueOf(table.getShipPayType()));
			tv_ShipWeight.setText(String.valueOf(table.getShipWeightNow() - table.getShipWeightOri()));
		}
		
		tv_WeightNow.setText(String.valueOf(table.getShipWeightNow()));
		tv_LoadType.setText(ShipAttrUtil.shipLoadType(table.getShipLoadType()));
		tv_SafeLevel.setText(ShipAttrUtil.shipSafeLevel(table.getShipSafeLevel()));
		tv_SailStart.setText(table.getShipSailStart());
		tv_SailEnd.setText(table.getShipSailEnd());
		tv_Sail_Orientation.setText(ShipAttrUtil.shipOrientation(table.getShipSailOrientation()));
		tv_GoodsName.setText(table.getShipGoodsName());
		
		tv_ShipName.setText(table.getShipName());
		tv_ShipType.setText(ShipAttrUtil.shipTypeInt2String(table.getShipType()));
		tv_ShipWeightOri.setText(String.valueOf(table.getShipWeightOri()));
		tv_RegisterNum.setText(table.getShipRegisterNum());
	}

	private void setMoneyPanelVisibility() {
		switch (process) {
		case 0:
			linearLayout_MoneyDetail.setVisibility(View.GONE);
			break;

		default:
			break;
		}
	}
	/** 设置顶部按钮的显示文字 */
	private void setTopButtonText() {
		switch (process) {
		case 0:
			iv_Submit.setText(R.string.textview_layout_top_sub_text);
			break;
		case 1:
			iv_Submit.setText(R.string.pay_tip);
			break;
		case 2:
			break;
		case 3:
		case 4:
			iv_Submit.setVisibility(View.INVISIBLE);
			break;
		default:
			break;
		}
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_TopReturn:
			finish();
			onDestroy();
			break;
		case R.id.layout_sub_Add:
			//此处演示需要
			if(process == 0){
				table.setShipProcess(1);
				table.setShipPayType(1);
				table.setShipPayMoney(200);
				table.setShipTicketNumber("A00000006");
			} else if(process == 1){
				table.setShipProcess(4);
			}
			DataBaseHelper.getInstance().getUserRecord().updata(table);
			finish();
			onDestroy();
			break;

		default:
			break;
		}
	}
	
	
}
